package top.xiangqifu.springboot2_auth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.xiangqifu.springboot2_auth.base.BasePageResult;
import top.xiangqifu.springboot2_auth.base.BaseResult;
import top.xiangqifu.springboot2_auth.dao.TbAuthUserDAO;
import top.xiangqifu.springboot2_auth.dao.TbPortableMessageDAO;
import top.xiangqifu.springboot2_auth.entity.AuthInfo;
import top.xiangqifu.springboot2_auth.entity.PageBean;
import top.xiangqifu.springboot2_auth.entity.TbAuthUser;
import top.xiangqifu.springboot2_auth.entity.TbPortableMessage;
import top.xiangqifu.springboot2_auth.service.TbPortableMessageService;
import top.xiangqifu.springboot2_auth.utils.AuthInfoUtils;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor
public class TbPortableMessageServiceImpl implements TbPortableMessageService {

    private final String CONCERN="关注组";

    private final TbAuthUserDAO tbAuthUserDAO;
    private final TbPortableMessageDAO tbPortableMessageDAO;

    @Override
    public BaseResult concernadd(TbAuthUser user){
        //获取拦截器中的用户id
        AuthInfo authInfo= AuthInfoUtils.authInfo();
        //获取前端传过来的用户信息
        TbAuthUser user1=tbAuthUserDAO.queryByName(user);
        //创建门户表填充获取到的数据
        TbPortableMessage tbPortableMessage=new TbPortableMessage();
        //进行关注操作的用户
        tbPortableMessage.setAccessKey(authInfo.getUserId().toString());
        tbPortableMessage.setMessageGroup(CONCERN);
        //被关注的用户信息
        tbPortableMessage.setMessageKey(user1.getUserId().toString());
        tbPortableMessage.setMessage("");
        //查询是否有关注过该用户
        TbPortableMessage portableMessage=tbPortableMessageDAO.queryconcern(tbPortableMessage);
        if (portableMessage!=null){
            //进入代表已经关注过，进行取消关注操作
            int i=tbPortableMessageDAO.concerndelete(portableMessage.getPmid());
            if (i==1) {
                return BaseResult.ok("取消关注成功");
            }
                return BaseResult.fail("取消关注失败");

            }
        //进入这里表示没有关注过，进行关注操作
        int  i=tbPortableMessageDAO.concernadd(tbPortableMessage);
        if (i==1){
            return BaseResult.ok("关注用户成功");
        }
        return BaseResult.fail("关注用户失败");
    }
    //查询关注我的用户列表
    @Override
    public BasePageResult<List<TbPortableMessage>> queryList(PageBean pageBean) {
        //分页处理
        pageBean = Optional.ofNullable(pageBean).orElse(new PageBean());
        //启动分页处理
        PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
        //获取当前用户信息
        AuthInfo authInfo = AuthInfoUtils.authInfo();
        //查询条件赋值
        TbPortableMessage message=new TbPortableMessage();
        message.setMessageKey(authInfo.getUserId().toString());
        message.setMessageGroup(CONCERN);
        //通过条件获取符合条件的用户数据
        List<TbAuthUser> list = tbAuthUserDAO.queryList(message);
        PageInfo<TbAuthUser> pageInfo = new PageInfo<TbAuthUser>(list);
        pageBean.setPageInfo(pageInfo);
        BasePageResult result = new BasePageResult();
        result.setSuccess(true);
        result.setCode(200);
        result.setMessage("");
        result.setPage(pageBean);
        result.setData(list);
        return result;
    }


    @Override
    public BasePageResult<List<TbPortableMessage>> queryuserList(TbAuthUser user, PageBean pageBean) {
        //分页处理
        pageBean = Optional.ofNullable(pageBean).orElse(new PageBean());
        //启动分页处理
        PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
        //通过前端传递的姓名获取用户id
        TbAuthUser user1=tbAuthUserDAO.queryByName(user);
        //查询条件赋值
        TbPortableMessage message=new TbPortableMessage();
        message.setMessageKey(user1.getUserId().toString());
        message.setMessageGroup(CONCERN);
        //通过条件获取符合条件的用户数据
        List<TbAuthUser> list = tbAuthUserDAO.queryList(message);
        PageInfo<TbAuthUser> pageInfo = new PageInfo<TbAuthUser>(list);
        pageBean.setPageInfo(pageInfo);
        BasePageResult result = new BasePageResult();
        result.setSuccess(true);
        result.setCode(200);
        result.setMessage("");
        result.setPage(pageBean);
        result.setData(list);
        return result;
    }

    //查询关注我的用户列表
    @Override
    public BasePageResult<List<TbPortableMessage>> querynoList(PageBean pageBean) {
        //分页处理
        pageBean = Optional.ofNullable(pageBean).orElse(new PageBean());
        //启动分页处理
        PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
        //获取当前用户信息
        AuthInfo authInfo = AuthInfoUtils.authInfo();
        //查询条件赋值
        TbPortableMessage message=new TbPortableMessage();
        message.setAccessKey(authInfo.getUserId().toString());
        message.setMessageGroup(CONCERN);
        //通过条件获取符合条件的用户数据
        List<TbAuthUser> list = tbAuthUserDAO.querynoList(message);
        PageInfo<TbAuthUser> pageInfo = new PageInfo<TbAuthUser>(list);
        pageBean.setPageInfo(pageInfo);
        BasePageResult result = new BasePageResult();
        result.setSuccess(true);
        result.setCode(200);
        result.setMessage("");
        result.setPage(pageBean);
        result.setData(list);
        return result;
    }

    @Override
    public BasePageResult<List<TbPortableMessage>> queryusernoList(TbAuthUser user, PageBean pageBean) {
        //分页处理
        pageBean = Optional.ofNullable(pageBean).orElse(new PageBean());
        //启动分页处理
        PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
        //通过用户账号获取用户id
        TbAuthUser user1=tbAuthUserDAO.queryByName(user);
        //查询条件赋值
        TbPortableMessage message=new TbPortableMessage();
        message.setAccessKey(user1.getUserId().toString());
        message.setMessageGroup(CONCERN);
        //通过条件获取符合条件的用户数据
        List<TbAuthUser> list = tbAuthUserDAO.querynoList(message);
        PageInfo<TbAuthUser> pageInfo = new PageInfo<TbAuthUser>(list);
        pageBean.setPageInfo(pageInfo);
        BasePageResult result = new BasePageResult();
        result.setSuccess(true);
        result.setCode(200);
        result.setMessage("");
        result.setPage(pageBean);
        result.setData(list);
        return result;
    }

}
